/**
 * 统计包含客厅、餐厅的户型所占比例
 */

const globalConf = require('../../../config/config.prod.json');
const fs = require('fs-extra');
const path = require('path');
const { generateXLSX } = require('../../utils/tool');
const MongoDatabase = require('../../Mongo/index');
const MysqlDatabase = require('../../Model/index');
const { fetchData, fetchMysqlFloorplanData, fetchMysqlFloorplanImageData } = require('./rule/index');
const TARGET_PATH = path.resolve(__dirname, '../../../output/');

function mapModelIdAndModelVersion(dataList, modelList) {
	return dataList.map((data) => {
		const modelData = modelList.find(item => item.fpid === data.fpid);

		if (modelData) {
			return {
				...data,
				model_id: modelData.modelId,
				model_version: modelData.modelVersion
			};
		}
	});
}


function mapFloorplanColorImage(dataList, imageList) {
	return dataList.map((data) => {
		const imageData = imageList.find(item => {
			return item.fpid === data.fpid;
		});

		if (imageData) {
			return {
				...data,
				url: imageData.url
			};
		}

		return {
			...data,
			url: ''
		};
	});
}

(async () => {
	const { mongo: mongoOptions, mysql: mysqlOptions } = globalConf;

	// 数据查询配置
	const searchOptions = globalConf['search-condition']['analize-keting-canting'];

	const MongoModel = await MongoDatabase(mongoOptions);
	const MysqlModel = await MysqlDatabase(mysqlOptions);

	const floorplanList = await fetchData(MongoModel.Drawing, {
		...searchOptions
	});

	const modelDataList = await fetchMysqlFloorplanData(MysqlModel.Model.Floorplan, {
		fpidList: floorplanList.map(item => item.fpid)
	});

	const imageList = await fetchMysqlFloorplanImageData(MysqlModel.Model.FloorplanImage, {
		type: 'COLOR',
		level: 0,
		fpidList: floorplanList.map(item => item.fpid)
	});


	// 数据分析
	floorplanList.forEach(model => {
		const drawing = model.drawing;
		const floorList = drawing.floor.list;

		for (const floor of floorList) {
			const { roomList: roomListExt = [] } = floor;

			const roomIndex1 = roomListExt.findIndex(item => {
				return item.type === '客厅';
			});

			const roomIndex2 = roomListExt.findIndex(item => {
				return item.type === '餐厅';
			});

			if (roomIndex1 === -1 || roomIndex2 === -1) {
				model._hasKetingCanting = false;

				break;
			}

			model._hasKetingCanting = true;

			break;
		}
	});

	floorplanList.forEach(model => {
		const drawing = model.drawing;
		const floorList = drawing.floor.list;

		for (const floor of floorList) {
			const { roomList: roomListExt = [] } = floor;

			const roomIndex = roomListExt.findIndex(item => {
				return item.type === '客餐厅';
			});

			if (roomIndex !== -1) {
				model._hasKecanting = true;

				break;
			}

			model._hasKecanting = false;

			break;
		}
	});

	// 有客厅和餐厅
	const hasKetingCantingFloorplanList = floorplanList.filter(floorplan => floorplan._hasKetingCanting);
	const proportion = (parseFloat(hasKetingCantingFloorplanList.length / floorplanList.length)) * 100 + '%';


	console.log('客厅餐厅户型占有率:' + proportion);

	// 有客餐厅
	const hasKecanting = floorplanList.filter(floorplan => floorplan._hasKecanting);
	const _proportion = (parseFloat(hasKecanting.length / floorplanList.length)) * 100 + '%';

	console.log('客餐厅户型占有率:' + _proportion);


	let dataList = floorplanList.map(data => {
		return {
			fpid: data.fpid,
			is_valid: data._hasKetingCanting ? '是' : '否',
			has_kecanting: data._hasKecanting ? '是' : '否',
			model_id: '',
			model_version: '',
			url: data.url
		};
	});

	dataList = mapModelIdAndModelVersion(dataList, modelDataList);

	dataList = mapFloorplanColorImage(dataList, imageList);

	const _dataList = dataList.map((data) => {
		const values = Object.values(data);

		return values.map(iv => {
			if (Array.isArray(iv)) {
				return iv.join(',');
			}

			return iv;
		});
	});

	_dataList.unshift(['户型图ID', '有客厅和餐厅', '有客餐厅', '模型ID', '模型版本', '图片地址']);
	_dataList.unshift([
		`
		统计总数: ${floorplanList.length},
		有客厅和餐厅的数量: ${hasKetingCantingFloorplanList.length},
		有客餐厅的数量：${hasKecanting.length},
		无厅的数量: ${floorplanList.length - hasKetingCantingFloorplanList.length - hasKecanting.length}
		`
	]);

	await fs.ensureDir(TARGET_PATH);

	await generateXLSX(_dataList, TARGET_PATH + '/' + `【客厅餐厅】户型占比统计_${Date.now()}.xlsx`);

	process.exit(0);
})();